package controller;

import domain.Client;
import dto.*;
import java.net.ConnectException;

/**
 * The controller that handles account related stuff
 *
 * @author Joachim
 */
public class AccountController {

    private Client client;

    /**
     * Constructor
     *
     * @param client The client to whom the controller should talk to
     */
    public AccountController(Client client) {
        this.client = client;
    }

    /**
     * Calls the client to retrieve the email address from the settings.
     *
     * @return The email address
     */
    public String getEmail() {
        return client.getEmail();
    }

    /**
     * Calls the client to log in a user.
     *
     * @param dto DTO with the email address & password.
     * @return True or false to show whether log in was successful or not.
     * @throws IllegalArgumentException If validation fails (empty fields,
     * invalid email).
     * @throws ConnectException If connection with the server fails.
     */
    public boolean logIn(LoginDTO dto) throws IllegalArgumentException, ConnectException {
        return client.logIn(dto.getEmail(), dto.getPassword());
    }

    /**
     * Calls the client to register a user
     *
     * @param dto DTO with the email address, password & repeat password
     * @return True or false to show whether registration was successful or not.
     * @throws IllegalArgumentException If validation fails (empty fields,
     * invalid email, passwords don't match).
     * @throws ConnectException If connection with the server fails.
     */
    public boolean register(RegisterDTO dto) throws IllegalArgumentException, ConnectException {
        return client.register(dto.getEmail(), dto.getPassword(), dto.getRepeatPassword());
    }

    /**
     * Calls the client to reset the password from a user
     *
     * @param dto DTO with the email address.
     * @return True or false to show whether registration was successful or not.
     * @throws IllegalArgumentException If validation fails (invalid email).
     * @throws ConnectException If connection with the server fails.
     */
    public boolean resetPassword(ResetPasswordDTO dto) throws IllegalArgumentException, ConnectException {
        return client.resetPassword(dto.getEmail());
    }

}
